<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      let arr = [
        {
          children: [
            {
              children: [
                {
                  dir: '',
                  domain: [],
                  id: 11,
                  name: '南昌市人民政府',
                  status: 0,
                  children: [
                    {
                      dir: '',
                      domain: [],
                      id: 111,
                      name: '南昌市人民政府',
                      status: 0,
                      children: []
                    },
                    {
                      dir: '',
                      domain: [],
                      id: 112,
                      name: '南昌市人民政府',
                      status: 0,
                      children: []
                    },
                    {
                      dir: '',
                      domain: [],
                      id: 113,
                      name: '南昌市人民政府',
                      status: 0,
                      children: []
                    },
                    {
                      dir: '',
                      domain: [],
                      id: 114,
                      name: '南昌市人民政府',
                      status: 0,
                      children: []
                    }
                  ]
                },
                {
                  dir: '',
                  domain: [],
                  id: 12,
                  name: '九江市人民政府',
                  status: 0
                },
                {
                  dir: '',
                  domain: [],
                  id: 13,
                  name: '景德镇市人民政府',
                  status: 0
                },
                {
                  dir: '',
                  domain: [],
                  id: 14,
                  name: '赣州市人民政府',
                  status: 0
                }
              ],
              dir: '',
              domain: [],
              id: 1,
              name: '江西省人民政府',
              status: 0
            },
            {
              children: [
                {
                  dir: '',
                  domain: [],
                  id: 21,
                  name: '长沙市人民政府',
                  status: 0
                },
                {
                  dir: '',
                  domain: [],
                  id: 22,
                  name: '株洲市人民政府',
                  status: 0
                },
                {
                  dir: '',
                  domain: [],
                  id: 23,
                  name: '景德镇市人民政府',
                  status: 0
                },
                {
                  dir: '',
                  domain: [],
                  id: 24,
                  name: '赣州市人民政府',
                  status: 0
                }
              ],
              dir: '',
              domain: [],
              id: 2,
              name: '湖南省人民政府',
              status: 0
            },
            {
              children: [
                {
                  dir: '',
                  domain: [],
                  id: 31,
                  name: '长沙市人民政府',
                  status: 0
                },
                {
                  dir: '',
                  domain: [],
                  id: 32,
                  name: '株洲市人民政府',
                  status: 0
                },
                {
                  dir: '',
                  domain: [],
                  id: 33,
                  name: '景德镇市人民政府',
                  status: 0
                },
                {
                  dir: '',
                  domain: [],
                  id: 34,
                  name: '赣州市人民政府',
                  status: 0
                }
              ],
              dir: '',
              domain: [],
              id: 3,
              name: '山东省人民政府',
              status: 0
            }
          ],
          dir: '',
          domain: [],
          id: 0,
          name: '中华人民共和国',
          status: 0
        }
      ]
      const flatten = (arr) => {
        var arr = arr.reduce(
          (arr, val) =>
            arr.concat(
              Array.isArray(val.children) ? flatten(val.children) : val
            ),
          []
        )
        return arr
      }

      const addKye = (arr) => {
        arr.map((v, i) => {
          if (Array.isArray(v.children)) {
            if (v.children.length !== 0) {
              v.key = 'aaaaa'
            } else {
              addKye(v.children)
            }
          } else {
            v.key = 'aaaaa'
          }
        })
        return arr
      }
      console.log(addKye(arr))
    </script>
  </body>
</html>
